<?php

namespace App\Http\Controllers;

use App\Models\BannerModel;
use App\Models\MemberDocModel;
use App\Models\NoticeModel;
use App\Models\QiniuConfigModel;
use Illuminate\Http\Request;
use App\Models\BannerModel as Banner;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class BannerNoticeController extends Controller
{

    public function memberDocList(Request $request)
    {
        $data = MemberDocModel::get();
        return self::returnMsg($data);
    }

    public function memberDocEdit(Request $request)
    {
        $Id      = (int)$request->input('id');
        $Content = $request->input('content');
        $data    = MemberDocModel::get_by_id($Id);
        if (!isset($data))
            return self::errorMsg('未找到该协议');
        $data->Content = $Content;
        try {
            $data->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::returnError(20001, $exception->getMessage());
        }
    }

    /**
     * @func获取Notice列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function noticeList(Request $request)
    {
        $count = intval($request->input('limit')) > 0 ? intval($request->input('limit')) : 20;
        $where = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->where('title', 'like', $keywords);
            }
        };

        $Noticelist = NoticeModel::where($where)->paginate($count);
        foreach ($Noticelist as $key => &$value) {
            $value['AddTimeName'] = date('Y-m-d H:i:s', $value['add_time']);
        }
        return self::returnMsg($Noticelist, '', 20000);
    }


    /**
     * @func更新Notice
     * @param Request $request
     */
    public function noticeAdd(Request $request)
    {
        $rules = [
            'title'   => 'required',
            'content' => 'required',
        ];
        $v     = Validator::make($request->all(), $rules);
        if ($v->fails()) return self::returnError(20001, $v->errors()->first());
        $sqlmap = $v->validated();

        $notice           = new NoticeModel();
        $notice->title    = $sqlmap['title'];
        $notice->content  = $sqlmap['content'];
        $notice->add_time = time();

        try {
            $notice->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }

    }


    /**
     * @func更新Notice
     * @param Request $request
     */
    public function noticeUpdate(Request $request)
    {
        $rules = [
            'id'      => 'required|integer',
            'title'   => 'required',
            'content' => 'required',
        ];
        $v     = Validator::make($request->all(), $rules);
        if ($v->fails())
            return self::errorMsg($v->errors()->first());
        $sqlmap = $v->validated();

        $notice = NoticeModel::find($sqlmap['id']);
        if (!isset($notice))
            return self::errorMsg('该文章不存在');
        $notice->title    = $sqlmap['title'];
        $notice->content  = $sqlmap['content'];
        $notice->add_time = time();
        try {
            $notice->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }


    /** @func删除Notice
     * @param Request $request
     */
    public function noticeDelete(Request $request)
    {
        $id   = (int)$request->input('id');
        $data = NoticeModel::find($id);
        if (!isset($data))
            return self::errorMsg('该文章不存在');

        try {
            $data->delete();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }

    /**
     * @func根据id查找数据
     */
    public function getNotice(Request $request)
    {
        $id   = (int)$request->input('id');
        $data = NoticeModel::find($id);
        return self::returnMsg($data);
    }


    /**
     * @func获取banner列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function bannerList(Request $request)
    {
        $count      = intval($request->input('limit')) > 0 ? intval($request->input('limit')) : 10;
        $where      = function ($query) use ($request) {
            //筛选查询关键字
            if ($request->has('keywords') and $request->keywords != '') {
                $keywords = "%" . $request->keywords . "%";
                $query->where('name', 'like', $keywords);
            }
        };
        $bannerlist = Banner::GetPageList($count, $where);
        return self::returnMsg($bannerlist, '', 20000);
    }


    /**
     * @func更新banner
     * @param Request $request
     */
    public function bannerUpdate(Request $request)
    {

        $rules = [
            'sort'  => 'required|integer',
            'name'  => 'required',
            'image' => 'required',
            'url'   => 'max:255'
        ];
        $v     = Validator::make($request->all(), $rules);
        if ($v->fails())
            return self::errorMsg($v->errors()->first());
        $sqlmap = $v->validated();

        $id = (int)$request->input('id');
        if ($id > 0) {
            $banner = BannerModel::find($id);
            if (!isset($banner))
                return self::errorMsg('该banner不存在');
        } else {
            $banner = new BannerModel();
        }

        $banner->name  = $sqlmap['name'];
        $banner->sort  = $sqlmap['sort'];
        $banner->image = $sqlmap['image'];
        $banner->url   = isset($sqlmap['url']) ? $sqlmap['url'] : null;
        try {
            $banner->save();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }


    /** @func删除banner
     * @param Request $request
     */
    public function bannerDelete(Request $request)
    {
        $id         = (int)$request->input('id');
        $banner_arr = BannerModel::find($id);
        if (!isset($banner_arr))
            return self::errorMsg('数据不存在,或者已经被删除');

//        $banner_arr->IsDel = 1;
        try {
//            $res = $banner_arr->save();
            $banner_arr->delete();
            return self::successMsg();
        } catch (\Exception $exception) {
            return self::errorMsg($exception->getMessage());
        }
    }


    /**
     * @func根据id查找数据
     */
    public function getBanner(Request $request)
    {
        $id = (int)$request->input('id');
        if (empty($id))
            return self::returnMsg([], 'id不能为空', 20003);

        $banner_arr          = Banner::get_by_id($id);
        $banner_arr['Image'] = $this->config['Domain'] . '/' . $banner_arr['Image'];
        return self::returnMsg($banner_arr);
    }


}
